{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e1fb2654",
   "metadata": {},
   "source": [
    "# Simple Image Generator with OpenAI DALL-E\n",
    "\n",
    "Generate beautiful images using AI! Choose a random theme or create your own custom prompt.\n",
    "Costs a bit more careful!\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f856bd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import libraries\n",
    "import os\n",
    "from openai import OpenAI\n",
    "from PIL import Image\n",
    "import requests\n",
    "from io import BytesIO\n",
    "\n",
    "# Get OpenAI API key\n",
    "api_key = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "if not api_key:\n",
    "    print(\"OPENAI_API_KEY not found in environment.\")\n",
    "    api_key = input(\"Please enter your OpenAI API key: \").strip()\n",
    "\n",
    "# Initialize OpenAI client\n",
    "client = OpenAI(api_key=api_key)\n",
    "print(\"✓ OpenAI client initialized!\")\n",
    "print(\"Ready to generate images!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c49df89",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Cell 2: Define themes\n",
    "import random\n",
    "\n",
    "# List of fun themes to choose from\n",
    "themes = [\n",
    "    \"a cyberpunk city at night with neon lights\",\n",
    "    \"a magical fantasy forest with glowing mushrooms\",\n",
    "    \"a cute robot having tea in a garden\",\n",
    "    \"an astronaut riding a horse on Mars\",\n",
    "    \"a cozy cabin in snowy mountains at sunset\",\n",
    "    \"a steampunk flying machine above clouds\",\n",
    "    \"a peaceful Japanese garden in autumn\",\n",
    "    \"a dragon sleeping on a pile of books\",\n",
    "    \"an underwater city with colorful fish\",\n",
    "    \"a vintage car on a desert highway at golden hour\",\n",
    "    \"a cozy coffee shop on a rainy day\",\n",
    "    \"a majestic phoenix rising from flames\",\n",
    "    \"a futuristic space station orbiting Earth\",\n",
    "    \"a whimsical treehouse village in the clouds\",\n",
    "    \"a serene Buddhist temple on a mountain peak\"\n",
    "]\n",
    "\n",
    "print(\"Available themes loaded!\")\n",
    "print(f\"Total themes: {len(themes)}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31775d64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Cell 3: Get user input\n",
    "print(\"IMAGE GENERATOR - Choose Your Option\")\n",
    "\n",
    "choice = input(\"Type 'random' for a random theme or 'custom' for your own prompt: \").strip().lower()\n",
    "\n",
    "if choice == 'random':\n",
    "    prompt = random.choice(themes)\n",
    "    print(f\"\\n Random theme selected!\")\n",
    "    print(f\"Prompt: {prompt}\")\n",
    "else:\n",
    "    prompt = input(\"\\nEnter your custom image prompt: \").strip()\n",
    "    if not prompt:\n",
    "        prompt = random.choice(themes)\n",
    "        print(f\"No input provided, using random theme: {prompt}\")\n",
    "    else:\n",
    "        print(f\"Using your custom prompt: {prompt}\")\n",
    "\n",
    "print(\"\\n✓ Prompt ready for generation!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3cdc5ddc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Cell 4: Generate and display image\n",
    "print(\"\\n Generating image...\")\n",
    "print(\"This may take 10-30 seconds...\")\n",
    "\n",
    "try:\n",
    "    # Call DALL-E API\n",
    "    response = client.images.generate(\n",
    "        model=\"dall-e-3\",\n",
    "        prompt=prompt,\n",
    "        size=\"1024x1024\",\n",
    "        quality=\"standard\",\n",
    "        n=1,\n",
    "    )\n",
    "    \n",
    "    # Get image URL\n",
    "    image_url = response.data[0].url\n",
    "    \n",
    "    print(\"✓ Image generated successfully!\")\n",
    "    print(f\"\\nImage URL: {image_url}\")\n",
    "    \n",
    "    # Download and display image\n",
    "    print(\"\\nDownloading image...\")\n",
    "    response = requests.get(image_url)\n",
    "    img = Image.open(BytesIO(response.content))\n",
    "    \n",
    "    print(\"✓ Image ready!\")\n",
    "    print(\"\\nDisplaying image below:\")\n",
    "    display(img)\n",
    "    \n",
    "    # Store for optional saving later\n",
    "    generated_image = img\n",
    "    \n",
    "except Exception as e:\n",
    "    print(f\"❌ Error generating image: {str(e)}\")\n",
    "    generated_image = None\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ead2c877",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
